import { userGetInfoService } from "@/api/user";
import { defineStore } from "pinia";
import { ref } from 'vue'

// 用户模块 token setToken removeToken --> 并完成持久化
export const useUserStore = defineStore('big-user', () => {
    const localStorageToken = localStorage.getItem('token')
    const token = localStorageToken !== null ? ref(localStorageToken) : ref('')
    const setToken = (newToken) => {
        token.value = newToken
        localStorage.setItem('token', newToken)
    }
    const removeToken = () => {
        token.value = ''
        localStorage.removeItem('token')
    }

    const user = ref({})
    const getUser = async () => {
        const res = await userGetInfoService()  // 请求获取数据
        console.log("res:" + res)
        user.value = res.data.data
    }
    const setUser = (obj) => {
        user.value = obj
    }
    return {
        token,
        setToken,
        removeToken,
        getUser,
        setUser
    }
})